package org.example.web.controller;

import org.example.web.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class WebServiceController {

	private AccountService accountService;

	public WebServiceController(AccountService accountService) {
		this.accountService = accountService;
	}

	/**
	 * 探针检查响应类
	 * @return
	 */
	@RequestMapping("/health")
	public String health() {
		return "OK";
	}

	/**
	 * 远程调用account-service提供的服务
	 * @return 多次远程调返回的所有结果.
	 */
	@RequestMapping("/account")
	public String account() {

		StringBuilder sbud = new StringBuilder();

		for (int i = 0; i < 10; i++) {
			sbud.append(accountService.getDataFromSpringCloudK8SProvider())
					.append("<br>");
		}

		return sbud.toString();
	}

	@RequestMapping("/")
	public String hello() {
		return "Hello World";
	}

	@Autowired
	private DiscoveryClient discoveryClient;

	@RequestMapping("/services")
	public List<String> services() {
		return this.discoveryClient.getServices();
	}

}
